﻿#region License
// Copyright © 2004 - 2010 All Right Reserved, Derek Goslin
// 
// 1. Definitions
// The terms "reproduce," "reproduction," "derivative works," and "distribution" have the same meaning here as under U.S. copyright law.
// A "contribution" is the original software, or any additions or changes to the software.
// A "contributor" is any person that distributes its contribution under this license.
// "Licensed patents" are a contributor's patent claims that read directly on its contribution.
// 
// 2. Grant of Rights
// (A) Copyright Grant- Subject to the terms of this license, including the license
//     conditions and limitations in section 3, each contributor grants you a non-exclusive,
//     worldwide, royalty-free copyright license to reproduce its contribution, prepare derivative 
//     works of its contribution, and distribute its contribution or any derivative works that you create.
// (B) Patent Grant- Subject to the terms of this license, including the license conditions and limitations
//     in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free license under its
//     licensed patents to make, have made, use, sell, offer for sale, import, and/or otherwise dispose of
//     its contribution in the software or derivative works of the contribution in the software.
// 
// 3. Conditions and Limitations
// (A) Reciprocal Grants- For any file you distribute that contains code from the software (in source code or binary format), 
//     you must provide recipients the source code to that file along with a copy of this license, which license will govern that file. 
//     You may license other files that are entirely your own work and do not contain code from the software under any terms you choose.
// (B) No Trademark License- This license does not grant you rights to use any contributors' name, logo, or trademarks.
// (C) If you bring a patent claim against any contributor over patents that you claim are infringed by the software,
//     your patent license from such contributor to the software ends automatically.
// (D) If you distribute any portion of the software, you must retain all copyright, patent, trademark,
//     and attribution notices that are present in the software.
// (E) If you distribute any portion of the software in source code form, you may do so only under this license by including a complete copy 
//     of this license with your distribution. If you distribute any portion of the software in compiled or object code form, you may only do 
//     so under a license that complies with this license.
// (F) The software is licensed "as-is." You bear the risk of using it. The contributors give no express warranties, guarantees or conditions.
//    You may have additional consumer rights under your local laws which this license cannot change. To the extent permitted under your
//    local laws, the contributors exclude the implied warranties of merchantability, fitness for a particular purpose and non-infringement.
#endregion

using System;
using System.Collections.Generic;
using DnEcuDiag.Ecus.Enums;
using DnEcuDiag.Ecus.Generic;
using DnEcuDiag.Ecus.Iso;
using DnEcuDiag.Factories;
using NUnit.Framework;

namespace DnEcuDiag.Tests
{
    [TestFixture]
    public class M2103Tests
    {
        private Kwp71Ecu _ecu;
        private IsoTestDevice _device;

        [TestFixtureSetUp]
        public void TestSetup()
        {
            _device = new IsoTestDevice
            {
                IdentificationData = new List<Byte[]>
                                                          {
                                                              new byte[]
                                                                  {
                                                                      0x0D, 0x02, 0xF6, 0x32, 0x32, 0x34, 0x34, 0x30, 0x32, 0x31, 0x36, 0x32, 0x30
                                                                  },
                                                              new byte[]
                                                                  {
                                                                      0x0D, 0x04, 0xF6, 0x30, 0x37, 0x37, 0x35, 0x35, 0x33, 0x37, 0x33, 0x30, 0x31
                                                                  },
                                                              new byte[]
                                                                  {
                                                                      0x0F, 0x06, 0xF6, 0x20, 0x35, 0x38, 0x34, 0x32, 0x36, 0x34, 0x36, 0x34, 0x20, 0x20, 0xFF
                                                                  }
                                                          }.AsReadOnly()
            };

            EcuDescriptor descriptor = EcuFactory.EcuList.Find(ecuDescriptor => ecuDescriptor.Name == "Bosch M2.10.3");

            _ecu = EcuFactory.CreateEcu(descriptor) as Kwp71Ecu;
            Assert.IsNotNull(_ecu);
            _ecu.DiagnosticInterface = _device;
            _ecu.Connect();
        }

        [Test]
        public void TestIdentification()
        {
            Assert.AreEqual(string.Empty, _ecu.EcuWarning);
            Assert.AreEqual("55 A7 86 85 97 9E 79", _ecu.IsoCode);
            Assert.AreEqual("026120442", _ecu.HardwareNumber);
            Assert.AreEqual(String.Empty, _ecu.HardwareVersionNumber);
            Assert.AreEqual("Bosch M2.10.3", _ecu.Model);
            Assert.AreEqual(string.Empty, _ecu.OmologationNumber);
            Assert.AreEqual(string.Empty, _ecu.ProductionDate);
            Assert.AreEqual(string.Empty, _ecu.ProgrammingDate);
            Assert.AreEqual(string.Empty, _ecu.SerialNumber);
            Assert.AreEqual(string.Empty, _ecu.SoftwareVersionNumber);
            Assert.AreEqual("ÿ  46462485", _ecu.DrawingNumber);
            Assert.AreEqual(" 103735577", _ecu.SoftwareNumber);
            Assert.AreEqual(0x10, _ecu.EcuAddress);
        }

        [Test]
        public void TestFaults()
        {
            _device.Faultcodeinfo = new List<byte[]>
                                         {
                                             new byte[]
                                                 {
                                                     0x0D, 0x12, 0xFC, 0x2C, 0x71, 0x00, 0x00, 0x40, 0x2D, 0x71, 0x00,
                                                     0x00, 0x40
                                                 }
                                             ,
                                             new byte[]
                                                 {
                                                     0x08, 0x14, 0xFC, 0xC8, 0x78, 0x51, 0xAE, 0x40
                                                 }
                                         };

            _ecu.ReadFaultCodes();

            Assert.AreEqual(3, _ecu.ActiveFaultCodes.Count);

            Console.WriteLine(_ecu.ActiveFaultCodes[0].FaultComponent);
            Console.WriteLine(_ecu.ActiveFaultCodes[0].FaultDescription);
            Assert.AreEqual("002C", _ecu.ActiveFaultCodes[0].FaultComponent);

            Assert.AreEqual(
                "Failed Component Code: Air Temperature Sensor\r\n" +
                "Error Code Status: Error effects CARB (lamp on)\r\n" +
                "Error Code Status: Error memorised after period of time\r\n" +
                "Error Code Status: Error Present\r\n" +
                "Error Code Status: Error not intermittant\r\n" +
                "Error Type: Short Circuit VBatt/Open Circuit\r\n" +
                "Engine Speed: 0 rpm\r\n" +
                "Engine Load: 0 ms\r\n" +
                "Error Count: 64\r\n",
                _ecu.ActiveFaultCodes[0].FaultDescription);

            Console.WriteLine(_ecu.ActiveFaultCodes[1].FaultDescription);
            Console.WriteLine(_ecu.ActiveFaultCodes[1].FaultDescription);
            Assert.AreEqual("002D", _ecu.ActiveFaultCodes[1].FaultComponent);

            Assert.AreEqual(
                "Failed Component Code: Engine Temperature Sensor\r\n" +
                "Error Code Status: Error effects CARB (lamp on)\r\n" +
                "Error Code Status: Error memorised after period of time\r\n" +
                "Error Code Status: Error Present\r\n" +
                "Error Code Status: Error not intermittant\r\n" +
                "Error Type: Short Circuit VBatt/Open Circuit\r\n" +
                "Engine Speed: 0 rpm\r\n" +
                "Engine Load: 0 ms\r\n" +
                "Error Count: 64\r\n",
                _ecu.ActiveFaultCodes[1].FaultDescription);

            Console.WriteLine(_ecu.ActiveFaultCodes[2].FaultDescription);
            Console.WriteLine(_ecu.ActiveFaultCodes[2].FaultDescription);
            Assert.AreEqual("00C8", _ecu.ActiveFaultCodes[2].FaultComponent);

            Assert.AreEqual(
                "Failed Component Code: Immobiliser\r\n" +
                "Error Code Status: Error effects CARB (lamp on)\r\n" +
                "Error Code Status: Error memorised after period of time\r\n" +
                "Error Code Status: Error Present\r\n" +
                "Error Code Status: Error not intermittant\r\n" +
                "Error Type: Error between Immobiliser and ECU\r\n" +
                "Immobiliser State: No code received or link interrupted\r\n" +
                "Immobiliser State: Key code ok\r\n" +
                "Immobiliser State: Non Virgin Ecu\r\n" +
                "Immobiliser State: Engine start enabled\r\n" +
                "Immobiliser State: Universal Code Received\r\n" +
                "Battery Voltage: 12.27 volts\r\n" +
                "Error Count: 64\r\n",
                _ecu.ActiveFaultCodes[2].FaultDescription);
        }

        [Test]
        public void TestSensor()
        {   
            Assert.AreEqual(46, _ecu.Sensors.Count);

            TestSensorValues();
        }

        [Test]
        public void TestPerformanceMeasurment()
        {
            Assert.IsNotNull(_ecu.SpeedSensor);
            Assert.IsNotNull(_ecu.RpmSensor);
        }

        [Test]
        public void TestActuators()
        {
            Assert.AreEqual(12, _ecu.Actuators.Count);

            foreach (IsoActuator actuator in _ecu.Actuators)
            {
                Assert.AreEqual(ActuatorActivationType.Ecu, actuator.ActivationType);
            }
        }

        private void TestSensorValues()
        {
            int i = 0;
            TimeStampedMeasurement tsm;

            IsoSensor li = _ecu.Sensors[i] as IsoSensor;
            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfe, 0x1 } };

            Console.WriteLine(string.Format("Testing Sensor: [{0}] {1}", i, _ecu.Sensors[i].Name));
            tsm = _ecu.ReadSensor(_ecu.Sensors[i]);
            Assert.AreEqual("True", tsm.Status);
            Assert.AreEqual(1, tsm.Measurement);

            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfe, 0x0 } };

            tsm = _ecu.ReadSensor(_ecu.Sensors[i]);
            Assert.AreEqual("False", tsm.Status);
            Assert.AreEqual(0, tsm.Measurement);

            i++;

            li = _ecu.Sensors[i] as IsoSensor;
            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfe, 0x2 } };

            Console.WriteLine(string.Format("Testing Sensor: [{0}] {1}", i, _ecu.Sensors[i].Name));
            tsm = _ecu.ReadSensor(_ecu.Sensors[i]);
            Assert.AreEqual("True", tsm.Status);
            Assert.AreEqual(1, tsm.Measurement);

            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfe, 0x0 } };

            tsm = _ecu.ReadSensor(_ecu.Sensors[i]);
            Assert.AreEqual("False", tsm.Status);
            Assert.AreEqual(0, tsm.Measurement);

            i++;

            li = _ecu.Sensors[i] as IsoSensor;
            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfe, 0x4 } };

            Console.WriteLine(string.Format("Testing Sensor: [{0}] {1}", i, _ecu.Sensors[i].Name));
            tsm = _ecu.ReadSensor(_ecu.Sensors[i]);
            Assert.AreEqual("True", tsm.Status);
            Assert.AreEqual(1, tsm.Measurement);

            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfe, 0x0 } };

            tsm = _ecu.ReadSensor(_ecu.Sensors[i]);
            Assert.AreEqual("False", tsm.Status);
            Assert.AreEqual(0, tsm.Measurement);

            i++;

            li = _ecu.Sensors[i] as IsoSensor;
            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfe, 0x8 } };

            Console.WriteLine(string.Format("Testing Sensor: [{0}] {1}", i, _ecu.Sensors[i].Name));
            tsm = _ecu.ReadSensor(_ecu.Sensors[i]);
            Assert.AreEqual("True", tsm.Status);
            Assert.AreEqual(1, tsm.Measurement);

            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfe, 0x0 } };

            tsm = _ecu.ReadSensor(_ecu.Sensors[i]);
            Assert.AreEqual("False", tsm.Status);
            Assert.AreEqual(0, tsm.Measurement);

            i++;

            li = _ecu.Sensors[i] as IsoSensor;
            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfe, 0x10 } };

            Console.WriteLine(string.Format("Testing Sensor: [{0}] {1}", i, _ecu.Sensors[i].Name));
            tsm = _ecu.ReadSensor(_ecu.Sensors[i]);
            Assert.AreEqual("True", tsm.Status);
            Assert.AreEqual(1, tsm.Measurement);

            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfe, 0x0 } };

            tsm = _ecu.ReadSensor(_ecu.Sensors[i]);
            Assert.AreEqual("False", tsm.Status);
            Assert.AreEqual(0, tsm.Measurement);

            i++;

            li = _ecu.Sensors[i] as IsoSensor;
            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfe, 0x20 } };

            Console.WriteLine(string.Format("Testing Sensor: [{0}] {1}", i, _ecu.Sensors[i].Name));
            tsm = _ecu.ReadSensor(_ecu.Sensors[i]);
            Assert.AreEqual("True", tsm.Status);
            Assert.AreEqual(1, tsm.Measurement);

            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfe, 0x0 } };

            tsm = _ecu.ReadSensor(_ecu.Sensors[i]);
            Assert.AreEqual("False", tsm.Status);
            Assert.AreEqual(0, tsm.Measurement);

            i++;

            li = _ecu.Sensors[i] as IsoSensor;
            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfe, 0x40 } };

            Console.WriteLine(string.Format("Testing Sensor: [{0}] {1}", i, _ecu.Sensors[i].Name));
            tsm = _ecu.ReadSensor(_ecu.Sensors[i]);
            Assert.AreEqual("True", tsm.Status);
            Assert.AreEqual(1, tsm.Measurement);

            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfe, 0x0 } };

            tsm = _ecu.ReadSensor(_ecu.Sensors[i]);
            Assert.AreEqual("False", tsm.Status);
            Assert.AreEqual(0, tsm.Measurement);

            i++;

            li = _ecu.Sensors[i] as IsoSensor;
            _device.SensorData = new List<Byte[]> { new byte[] { 0x5, 0x1, 0xfe, 0x01, 0x1 } };
            Console.WriteLine(string.Format("Testing Sensor: [{0}] {1}", i, _ecu.Sensors[i].Name));
            tsm = _ecu.ReadSensor(_ecu.Sensors[i]);
            Assert.AreEqual(10, tsm.Measurement);

            _device.SensorData = new List<Byte[]> { new byte[] { 0x5, 0x1, 0xfe, 0xF0, 0x0 } };

            tsm = _ecu.ReadSensor(li);
            Assert.AreEqual(9600, tsm.Measurement);

            _device.SensorData = new List<Byte[]> { new byte[] { 0x5, 0x1, 0xfe, 0x01, 0xFF } };

            tsm = _ecu.ReadSensor(li);
            Assert.AreEqual(2550, tsm.Measurement);
            Assert.AreEqual(0x2000, li.Id);

            i++;

            li = _ecu.Sensors[i] as IsoSensor;

            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfe, 0x43 } };

            Console.WriteLine(string.Format("Testing Sensor : {0}", li.Name));
            tsm = _ecu.ReadSensor(li);
            Assert.AreEqual(10.2f, tsm.Measurement);
            Assert.AreEqual(0x2002, li.Id);

            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfe, 0x56 } };

            tsm = _ecu.ReadSensor(li);
            Assert.AreEqual(22.3f, tsm.Measurement);

            i++;

            li = _ecu.Sensors[i] as IsoSensor;

            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfe, 0x43 } };

            Console.WriteLine(string.Format("Testing Sensor : {0}", li.Name));
            tsm = _ecu.ReadSensor(li);
            Assert.AreEqual(10.2f, tsm.Measurement);
            Assert.AreEqual(0x2003, li.Id);

            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfe, 0x56 } };

            tsm = _ecu.ReadSensor(li);
            Assert.AreEqual(22.3f, tsm.Measurement);

            i++;

            li = _ecu.Sensors[i] as IsoSensor;

            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfe, 0x43 } };

            Console.WriteLine(string.Format("Testing Sensor : {0}", li.Name));
            tsm = _ecu.ReadSensor(li);
            Assert.AreEqual(0x43 * 96 / 230.4f, tsm.Measurement);
            Assert.AreEqual(0x2004, li.Id);

            i++;

            li = _ecu.Sensors[i] as IsoSensor;

            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfe, 0x43 } };

            Console.WriteLine(string.Format("Testing Sensor : {0}", li.Name));
            tsm = _ecu.ReadSensor(li);
            Assert.AreEqual(0x43 * 1.6f, tsm.Measurement);
            Assert.AreEqual(0x2005, li.Id);

            i++;

            li = _ecu.Sensors[i] as IsoSensor;

            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfe, 0x43 } };

            Console.WriteLine(string.Format("Testing Sensor : {0}", li.Name));
            tsm = _ecu.ReadSensor(li);
            Assert.AreEqual(0x43 * 0.2f, tsm.Measurement);
            Assert.AreEqual(0x2006, li.Id);

            i++;

            li = _ecu.Sensors[i] as IsoSensor;

            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfe, 0x43 } };

            Console.WriteLine(string.Format("Testing Sensor : {0}", li.Name));
            tsm = _ecu.ReadSensor(li);
            Assert.AreEqual(78 - 0x43 * 0.75f, tsm.Measurement);
            Assert.AreEqual(0x2007, li.Id);

            i++;

            li = _ecu.Sensors[i] as IsoSensor;

            _device.SensorData = new List<Byte[]> { new byte[] { 0x5, 0x1, 0xfe, 0x1, 0x43 } };

            Console.WriteLine(string.Format("Testing Sensor : {0}", li.Name));
            tsm = _ecu.ReadSensor(li);
            Assert.AreEqual(0x1 * 0.384f + 0x43 / 255f, tsm.Measurement);
            Assert.AreEqual(0x2008, li.Id);

            i++;

            li = _ecu.Sensors[i] as IsoSensor;

            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfe, 0x55 } };

            Console.WriteLine(string.Format("Testing Sensor : {0}", li.Name));
            tsm = _ecu.ReadSensor(li);
            Assert.AreEqual(0x55 * 96 / 230.4f, tsm.Measurement);
            Assert.AreEqual(0x200A, li.Id);

            i++;

            li = _ecu.Sensors[i] as IsoSensor;

            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfe, 0x43 } };

            Console.WriteLine(string.Format("Testing Sensor : {0}", li.Name));
            tsm = _ecu.ReadSensor(li);
            Assert.AreEqual(0x43 * 0.05f, tsm.Measurement);
            Assert.AreEqual(0x200B, li.Id);

            i++;

            li = _ecu.Sensors[i] as IsoSensor;

            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfe, 0x43 } };

            Console.WriteLine(string.Format("Testing Sensor : {0}", li.Name));
            tsm = _ecu.ReadSensor(li);
            Assert.AreEqual(0x43 * 0.2f, tsm.Measurement);
            Assert.AreEqual(0x200E, li.Id);

            i++;

            li = _ecu.Sensors[i] as IsoSensor;

            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfe, 0x43 } };

            Console.WriteLine(string.Format("Testing Sensor : {0}", li.Name));
            tsm = _ecu.ReadSensor(li);
            Assert.AreEqual(0x43 * 5 / 255f, tsm.Measurement);
            Assert.AreEqual(0x200F, li.Id);

            i++;

            li = _ecu.Sensors[i] as IsoSensor;

            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfe, 0x43 } };

            Console.WriteLine(string.Format("Testing Sensor : {0}", li.Name));
            tsm = _ecu.ReadSensor(li);
            Assert.AreEqual(0x43 * 0.0705f, tsm.Measurement);
            Assert.AreEqual(0x2010, li.Id);

            i++;

            li = _ecu.Sensors[i] as IsoSensor;

            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfe, 0x43 } };

            Console.WriteLine(string.Format("Testing Sensor : {0}", li.Name));
            tsm = _ecu.ReadSensor(li);
            Assert.AreEqual(0x43, tsm.Measurement);
            Assert.AreEqual(0x2011, li.Id);

            i++;

            li = _ecu.Sensors[i] as IsoSensor;

            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfe, 0x43 } };

            Console.WriteLine(string.Format("Testing Sensor : {0}", li.Name));
            tsm = _ecu.ReadSensor(li);
            Assert.AreEqual(0x43 * 1.25f / 255, tsm.Measurement);
            Assert.AreEqual(0x2016, li.Id);

            i++;

            li = _ecu.Sensors[i] as IsoSensor;
            Assert.AreEqual(0x201A, li.Id);

            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfe, 0x80 } };

            Console.WriteLine(string.Format("Testing Sensor : {0}", li.Name));
            tsm = _ecu.ReadSensor(li);
            Assert.AreEqual(0, tsm.Measurement);

            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfe, 0x0 } };

            tsm = _ecu.ReadSensor(li);
            Assert.AreEqual(-102.4f, tsm.Measurement);

            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfe, 0xff } };

            tsm = _ecu.ReadSensor(li);
            Assert.AreEqual(101.599998f, tsm.Measurement);

            i++;

            li = _ecu.Sensors[i] as IsoSensor;

            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfe, 0x80 } };

            Console.WriteLine(string.Format("Testing Sensor : {0}", li.Name));
            tsm = _ecu.ReadSensor(li);
            Assert.AreEqual(0, tsm.Measurement);
            Assert.AreEqual(0x201C, li.Id);

            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfe, 0x0 } };

            tsm = _ecu.ReadSensor(li);
            Assert.AreEqual(-256f, tsm.Measurement);

            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfe, 0xff } };

            tsm = _ecu.ReadSensor(li);
            Assert.AreEqual(254f, tsm.Measurement);

            i++;

            li = _ecu.Sensors[i] as IsoSensor;

            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfe, 0x01 } };

            Console.WriteLine(string.Format("Testing Sensor : {0}", li.Name));
            tsm = _ecu.ReadSensor(li);
            Assert.AreEqual("S.C to Gnd/Sensor lower limit", tsm.Status);
            Assert.AreEqual(1, tsm.Measurement);
            Assert.AreEqual(0x21, li.Id);

            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfe, 0x02 } };

            tsm = _ecu.ReadSensor(li);
            Assert.AreEqual("Lean", tsm.Status);
            Assert.AreEqual(2, tsm.Measurement);

            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfe, 0x03 } };

            tsm = _ecu.ReadSensor(li);
            Assert.AreEqual("Cold or Open Circuit", tsm.Status);
            Assert.AreEqual(3, tsm.Measurement);

            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfe, 0x04 } };

            tsm = _ecu.ReadSensor(li);
            Assert.AreEqual("Rich", tsm.Status);
            Assert.AreEqual(4, tsm.Measurement);

            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfe, 0x5 } };

            tsm = _ecu.ReadSensor(li);
            Assert.AreEqual("S.C to Vbat/Sensor upper limit", tsm.Status);
            Assert.AreEqual(5, tsm.Measurement);

            i++;

            li = _ecu.Sensors[i] as IsoSensor;

            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfe, 0x55 } };

            Console.WriteLine(string.Format("Testing Sensor : {0}", li.Name));
            tsm = _ecu.ReadSensor(li);
            Assert.AreEqual(0x55 * 10, tsm.Measurement);
            Assert.AreEqual(0x2022, li.Id);

            i++;

            li = _ecu.Sensors[i] as IsoSensor;

            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfe, 0x55 } };

            Console.WriteLine(string.Format("Testing Sensor : {0}", li.Name));
            tsm = _ecu.ReadSensor(li);
            Assert.AreEqual(0x55 * 10, tsm.Measurement);
            Assert.AreEqual(0x2023, li.Id);

            i++;

            li = _ecu.Sensors[i] as IsoSensor;

            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfe, 0x55 } };

            Console.WriteLine(string.Format("Testing Sensor : {0}", li.Name));
            tsm = _ecu.ReadSensor(li);
            Assert.AreEqual(0x55 * 0.39f, tsm.Measurement);
            Assert.AreEqual(0x202B, li.Id);

            i++;

            li = _ecu.Sensors[i] as IsoSensor;

            _device.SensorData = new List<Byte[]> { new byte[] { 0x5, 0x1, 0xfb, 0x0, 0x55 } };

            Console.WriteLine(string.Format("Testing Sensor : {0}", li.Name));
            tsm = _ecu.ReadSensor(li);
            Assert.AreEqual(0x55 * 96 / 230.4f, tsm.Measurement);
            Assert.AreEqual(0x0, li.Id);

            i++;

            li = _ecu.Sensors[i] as IsoSensor;

            _device.SensorData = new List<Byte[]> { new byte[] { 0x5, 0x1, 0xfb, 0x00, 0x55 } };

            Console.WriteLine(string.Format("Testing Sensor : {0}", li.Name));
            tsm = _ecu.ReadSensor(li);
            Assert.AreEqual(0x55 / 14.68f, tsm.Measurement);
            Assert.AreEqual(0x1, li.Id);

            i++;

            li = _ecu.Sensors[i] as IsoSensor;

            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfb, 0x0, 0x55 } };

            Console.WriteLine(string.Format("Testing Sensor : {0}", li.Name));
            tsm = _ecu.ReadSensor(li);
            Assert.AreEqual(0x55 * 5f / 255, tsm.Measurement);
            Assert.AreEqual(0x2, li.Id);

            i++;

            li = _ecu.Sensors[i] as IsoSensor;

            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfb, 0x0, 0x55 } };

            Console.WriteLine(string.Format("Testing Sensor : {0}", li.Name));
            tsm = _ecu.ReadSensor(li);
            Assert.AreEqual(66.0999985f, tsm.Measurement);
            Assert.AreEqual(0x3, li.Id);

            i++;

            li = _ecu.Sensors[i] as IsoSensor;

            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfb, 0x0, 0x55 } };

            Console.WriteLine(string.Format("Testing Sensor : {0}", li.Name));
            tsm = _ecu.ReadSensor(li);
            Assert.AreEqual(0x55 * 5f / 255, tsm.Measurement);
            Assert.AreEqual(0x4, li.Id);

            i++;

            li = _ecu.Sensors[i] as IsoSensor;

            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfb, 0x0, 0x55 } };

            Console.WriteLine(string.Format("Testing Sensor : {0}", li.Name));
            tsm = _ecu.ReadSensor(li);
            Assert.AreEqual(66.0999985f, tsm.Measurement);
            Assert.AreEqual(0x5, li.Id);

            i++;

            li = _ecu.Sensors[i] as IsoSensor;

            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfb, 0x0, 0x55 } };

            Console.WriteLine(string.Format("Testing Sensor : {0}", li.Name));
            tsm = _ecu.ReadSensor(li);
            Assert.AreEqual(0x55 * 5000f / 255, tsm.Measurement);
            Assert.AreEqual(0x6, li.Id);

            i++;

            li = _ecu.Sensors[i] as IsoSensor;

            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfb, 0x1, 0x55 } };

            Console.WriteLine(string.Format("Testing Sensor : {0}", li.Name));
            tsm = _ecu.ReadSensor(li);
            Assert.AreEqual(0x155 * 1250f / 255, tsm.Measurement);
            Assert.AreEqual(0x7, li.Id);

            i++;

            li = _ecu.Sensors[i] as IsoSensor;

            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfb, 0x0, 0x55 } };

            Console.WriteLine(string.Format("Testing Sensor : {0}", li.Name));
            tsm = _ecu.ReadSensor(li);
            Assert.AreEqual(0x55 * 5f / 255, tsm.Measurement);
            Assert.AreEqual(0x9, li.Id);

            i++;

            li = _ecu.Sensors[i] as IsoSensor;

            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfb, 0x0, 0xAA } };

            Console.WriteLine(string.Format("Testing Sensor : {0}", li.Name));
            tsm = _ecu.ReadSensor(li);
            Assert.AreEqual(0xAA * 5f / 255, tsm.Measurement);
            Assert.AreEqual(0xA, li.Id);
            i++;

            li = _ecu.Sensors[i] as IsoSensor;

            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfb, 0x0, 0x55 } };

            Console.WriteLine(string.Format("Testing Sensor : {0}", li.Name));
            tsm = _ecu.ReadSensor(li);
            Assert.AreEqual(0x55 / 14.68f, tsm.Measurement);
            Assert.AreEqual(0xB, li.Id);

            i++;

            li = _ecu.Sensors[i] as IsoSensor;

            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xFE, 0x1 } };

            Console.WriteLine(string.Format("Testing Sensor : {0}", li.Name));
            tsm = _ecu.ReadSensor(li);
            Assert.AreEqual(1f, tsm.Measurement);
            Assert.AreEqual("True", tsm.Status);
            Assert.AreEqual(0x20, li.Id);

            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfe, 0x0 } };

            tsm = _ecu.ReadSensor(li);
            Assert.AreEqual(0f, tsm.Measurement);
            Assert.AreEqual("False", tsm.Status);

            i++;

            li = _ecu.Sensors[i] as IsoSensor;

            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xFE, 0x2 } };

            Console.WriteLine(string.Format("Testing Sensor : {0}", li.Name));
            tsm = _ecu.ReadSensor(li);
            Assert.AreEqual(1f, tsm.Measurement);
            Assert.AreEqual("True", tsm.Status);
            Assert.AreEqual(0x20, li.Id);

            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfe, 0x0 } };

            tsm = _ecu.ReadSensor(li);
            Assert.AreEqual(0f, tsm.Measurement);
            Assert.AreEqual("False", tsm.Status);

            i++;

            li = _ecu.Sensors[i] as IsoSensor;

            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xFE, 0x4 } };

            Console.WriteLine(string.Format("Testing Sensor : {0}", li.Name));
            tsm = _ecu.ReadSensor(li);
            Assert.AreEqual(1f, tsm.Measurement);
            Assert.AreEqual("Gear Changed", tsm.Status);
            Assert.AreEqual(0x20, li.Id);

            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfe, 0x0 } };

            tsm = _ecu.ReadSensor(li);
            Assert.AreEqual(0f, tsm.Measurement);
            Assert.AreEqual("No Gear Change", tsm.Status);

            i++;

            li = _ecu.Sensors[i] as IsoSensor;

            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfe, 0x10 } };

            Console.WriteLine(string.Format("Testing Sensor : {0}", li.Name));
            tsm = _ecu.ReadSensor(li);
            Assert.AreEqual(1f, tsm.Measurement);
            Assert.AreEqual("Gearbox in Drive", tsm.Status);
            Assert.AreEqual(0x20, li.Id);

            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfe, 0x0 } };

            tsm = _ecu.ReadSensor(li);
            Assert.AreEqual(0f, tsm.Measurement);
            Assert.AreEqual("Gearbox not in Drive", tsm.Status);

            i++;

            li = _ecu.Sensors[i] as IsoSensor;

            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfe, 0x40 } };

            Console.WriteLine(string.Format("Testing Sensor : {0}", li.Name));
            tsm = _ecu.ReadSensor(li);
            Assert.AreEqual(1f, tsm.Measurement);
            Assert.AreEqual("Requested", tsm.Status);
            Assert.AreEqual(0x20, li.Id);

            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfe, 0x0 } };

            tsm = _ecu.ReadSensor(li);
            Assert.AreEqual(0f, tsm.Measurement);
            Assert.AreEqual("Not Requested", tsm.Status);

            i++;

            li = _ecu.Sensors[i] as IsoSensor;

            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfe, 0x80 } };

            Console.WriteLine(string.Format("Testing Sensor : {0}", li.Name));
            tsm = _ecu.ReadSensor(li);
            Assert.AreEqual(1f, tsm.Measurement);
            Assert.AreEqual("True", tsm.Status);
            Assert.AreEqual(0x20, li.Id);

            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfe, 0x0 } };

            tsm = _ecu.ReadSensor(li);
            Assert.AreEqual(0f, tsm.Measurement);
            Assert.AreEqual("False", tsm.Status);

            i++;

            li = _ecu.Sensors[i] as IsoSensor;

            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfe, 0x8 } };

            Console.WriteLine(string.Format("Testing Sensor : {0}", li.Name));
            tsm = _ecu.ReadSensor(li);
            Assert.AreEqual(1f, tsm.Measurement);
            Assert.AreEqual("Enabled", tsm.Status);
            Assert.AreEqual(0x21, li.Id);

            _device.SensorData = new List<Byte[]> { new byte[] { 0x4, 0x1, 0xfe, 0x0 } };

            tsm = _ecu.ReadSensor(li);
            Assert.AreEqual(0f, tsm.Measurement);
            Assert.AreEqual("Disabled", tsm.Status);
        }

        [TestFixtureTearDown]
        public void Dispose()
        {
            _ecu = null;
        }
    }
}
